#!/usr/bin/env python
# -*- coding: utf-8 -*-

import MySQLdb
import logging
import gc
from cPickle import load,dump

db = {}

def read_db():
    global db
    if 0:
        db = load(open('friends.db'))
        return

    #注意键是被关注的，值列表是关注者
    fcon = open('dbcontact')
    for i in fcon.readlines():
        fromto = i[:-1].split('\t')
        pfrom = fromto[0]
        pto = fromto[1]
        
        if db.get(pfrom) is None:
            db[pfrom] = [pto]
        else:
            db[pfrom].append(pto)

    logging.info('contact done')
    fcon = open('dbfriend')
    for i in fcon.readlines():
        fromto = i[:-1].split('\t')
        pfrom = fromto[0]
        pto = fromto[1]
        
        if db.get(pfrom) is None:
            db[pfrom] = [pto]
        else:
            db[pfrom].append(pto)

    logging.info('friends done')
    #dump(db, open('friends.db','w'))

def get_contacts(outfile):
    known = {}
    waitcheck = ['1112942']
    n = 0

    #gc.disable()
    while len(waitcheck):
    #for t in xrange(0,100):
        p = waitcheck.pop()
        all = db.get(p) or []
        
        for to_id in all:
            outfile.write('%s,%s\n'%(p,to_id))
            if (known.get(to_id) is None) and (len(to_id)>6):
            #if (to_id not in known) and (to_id not in waitcheck) and len(to_id)>6:
                waitcheck.append(to_id)
                known[to_id] = 1

        known[p] = 1
        #print p
        n += 1
        if n/100*100 == n:
            logging.info('%s people have finished'%n)

    #gc.enable()

def get_targets_of(p):
    return db.get(p) or []

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO,
            format='%(asctime)s %(levelname)-8s %(message)s',
            filename='fetch_con.log',
            )
    read_db()

    #import cProfile
    #cProfile.run("get_contacts(open('newcontacts.txt','w'))")
    get_contacts(open('newcontacts.txt','w'))

